﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace FingerprintAttendance
{
    public partial class CollectForm : Form
    {
        public CollectForm()
        {
            InitializeComponent();
        }

        private void CollectForm_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;

            cbbUserType.SelectedIndex = 0;

            dgvUsers.AutoGenerateColumns = false;
            dgvFingerprint.AutoGenerateColumns = false;
            //DataTable dt =DataOperation.GetUsers();
            //dgvUsers.DataSource = new BindingSource(dt, null);

           //DataTable dt = DataOperation.GetFingerprintSettings();
           // foreach (DataRow dr in dt.Rows)
           // {
           //     if (dr["No"].ToString() == "EnrollCount")
           //     {
           //         axZKFPEngX1.EnrollCount = int.Parse(dr["Value"].ToString());
           //         break;
           //     }
           // }

            //初始化指纹识别系统(0-初始化成功, 1-指纹识别驱动程序加载失败, 2-没有连接指纹识别仪, 3-属性SensorIndex指定的指纹仪不存在)
            //注:(多个设备的情况下,在调用InitEngine前设置此SensorIndex属性)            
            //if (axZKFPEngX1.InitEngine() != 0)
            //    MessageBox.Show("指纹采集仪初始化失败！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);

           
        }


        private void PictureBox_MouseEnter(object sender, EventArgs e)
        {
            PictureBox pb = (PictureBox)sender;
            pb.BorderStyle = BorderStyle.Fixed3D;

            foreach (Control cl in tableLayoutPanel1.Controls)
            {
                if (cl is PictureBox && cl != pb && ((PictureBox)cl).BorderStyle == BorderStyle.Fixed3D)
                    ((PictureBox)cl).BorderStyle = BorderStyle.FixedSingle;
            }
        }

        private void PictureBox_MouseLeave(object sender, EventArgs e)
        {
            PictureBox pb = (PictureBox)sender;
            pb.BorderStyle = BorderStyle.FixedSingle;
            //foreach (Control cl in tableLayoutPanel1.Controls)
            //{
            //    if (cl is PictureBox && cl != pb && ((PictureBox)cl).BorderStyle == BorderStyle.FixedSingle)
            //    {

            //        ((PictureBox)cl).BorderStyle = BorderStyle.Fixed3D;
            //    }
            //}
        }


        private void PictureBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            DataRow dr = CommonControl.CurrentRowInGrid(dgvUsers);
            if (dr == null)
            {
                MessageBox.Show("请选择相应的人员记录！","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }
            FingerprintForm fpf = new FingerprintForm();
            fpf.axZKFPEngX = this.axZKFPEngX1;
            switch (((PictureBox)sender).Name)
            {
                case "pbRightFinger1":
                    fpf.txtFingerName.Text = "右手大拇指";
                    fpf.FingerId = "R1";
                    break;
                case "pbRightFinger2":
                    fpf.txtFingerName.Text = "右手食指";
                    fpf.FingerId = "R2";
                    break;
                case "pbRightFinger3":
                    fpf.txtFingerName.Text = "右手中指";
                    fpf.FingerId = "R3";
                    break;
                case "pbRightFinger4":
                    fpf.txtFingerName.Text = "右手无名指";
                    fpf.FingerId = "R4";
                    break;
                case "pbRightFinger5":
                    fpf.txtFingerName.Text = "右手小拇指";
                    fpf.FingerId = "R5";
                    break;
                case "pbLeftFinger1":
                    fpf.txtFingerName.Text = "左手大拇指";
                    fpf.FingerId = "L1";
                    break;
                case "pbLeftFinger2":
                    fpf.txtFingerName.Text = "左手食指";
                    fpf.FingerId = "L2";
                    break;
                case "pbLeftFinger3":
                    fpf.txtFingerName.Text = "左手中指";
                    fpf.FingerId = "L3";
                    break;
                case "pbLeftFinger4":
                    fpf.txtFingerName.Text = "左手无名指";
                    fpf.FingerId = "L4";
                    break;
                case "pbLeftFinger5":
                    fpf.txtFingerName.Text = "左手小拇指";
                    fpf.FingerId = "L5";
                    break;
            }

           
            fpf.txtUserNumber.Text = dr["UserNumber"].ToString();
            fpf.txtUserName.Text = dr["UserName"].ToString();
            fpf.UserType = dr["UserType"].ToString();
            fpf.UserId = dr["UserId"].ToString();

            if (fpf.ShowDialog() == DialogResult.OK)
            {
                ((PictureBox)sender).Image = fpf.ImgFingerpring;
                DataTable dt = DataOperation.GetFingerprintByUser(dr["UserId"].ToString());
                dgvFingerprint.DataSource = new BindingSource(dt, null);
            }
        }

        private void CollectForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            //释放由方法InitEngine初始化的指纹设备 
            axZKFPEngX1.EndEngine();
        }

        private void dgvUsers_CurrentCellChanged(object sender, EventArgs e)
        {
            pbRightFinger1.Image = null;
            pbRightFinger2.Image = null;
            pbRightFinger3.Image = null;
            pbRightFinger4.Image = null;
            pbRightFinger5.Image = null;
            pbLeftFinger1.Image = null;
            pbLeftFinger2.Image = null;
            pbLeftFinger3.Image = null;
            pbLeftFinger4.Image = null;
            pbLeftFinger5.Image = null;


            string userId = CommonControl.CurrentRowInGrid(dgvUsers)["UserId"].ToString();

            DataTable dt = DataOperation.GetFingerprintByUser(userId);
            dgvFingerprint.DataSource = new BindingSource(dt, null);
            if (dt.Rows.Count == 0)
                return;
           
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["Image"] == DBNull.Value) continue;

                byte[] fpImg = (byte[])dr["Image"];
                MemoryStream ms = new MemoryStream(fpImg, true);
                Image img = new Bitmap(ms);

                ms.Close();

                switch (dr["FingerId"].ToString())
                {
                    case "R1":
                        pbRightFinger1.Image = img;
                        break;
                    case "R2":
                        pbRightFinger2.Image = img;
                        break;
                    case "R3":
                        pbRightFinger3.Image = img;
                        break;
                    case "R4":
                        pbRightFinger4.Image = img;
                        break;
                    case "R5":
                        pbRightFinger5.Image = img;
                        break;
                    case "L1":
                        pbLeftFinger1.Image = img;
                        break;
                    case "L2":
                        pbLeftFinger2.Image = img;
                        break;
                    case "L3":
                        pbLeftFinger3.Image = img;
                        break;
                    case "L4":
                        pbLeftFinger4.Image = img;
                        break;
                    case "L5":
                        pbLeftFinger5.Image = img;
                        break;
                }
            }

        }

      
        private void CollectForm_Activated(object sender, EventArgs e)
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(VerifyForm));
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));

            DataTable dt = DataOperation.GetFingerprintSettings();
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["No"].ToString() == "EnrollCount")
                {
                    axZKFPEngX1.EnrollCount = int.Parse(dr["Value"].ToString());
                    break;
                }
            }
            //初始化指纹识别系统(0-初始化成功, 1-指纹识别驱动程序加载失败, 2-没有连接指纹识别仪, 3-属性SensorIndex指定的指纹仪不存在)
            //注:(多个设备的情况下,在调用InitEngine前设置此SensorIndex属性)            
            if (axZKFPEngX1.InitEngine() != 0)
                MessageBox.Show("指纹采集仪初始化失败！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (dgvFingerprint.DataSource == null||dgvFingerprint.SelectedRows.Count==0)
                return;
            if (MessageBox.Show("请确认删除选择的指纹信息！", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK)
                return;

            ArrayList al = new ArrayList();

            foreach (DataGridViewRow dgvr in dgvFingerprint.Rows)
            {
                if (!dgvr.Selected) continue;
                DataRow dr = CommonControl.GetRowInGridAt(dgvFingerprint, dgvr.Index);

                DataOperation.DelFingerprintById(int.Parse(dr["FingerprintID"].ToString()));
               
                switch (dr["FingerId"].ToString())
                {
                    case "R1":
                        pbRightFinger1.Image = null;
                        break;
                    case "R2":
                        pbRightFinger2.Image = null;
                        break;
                    case "R3":
                        pbRightFinger3.Image = null;
                        break;
                    case "R4":
                        pbRightFinger4.Image = null;
                        break;
                    case "R5":
                        pbRightFinger5.Image = null;
                        break;
                    case "L1":
                        pbLeftFinger1.Image = null;
                        break;
                    case "L2":
                        pbLeftFinger2.Image = null;
                        break;
                    case "L3":
                        pbLeftFinger3.Image = null;
                        break;
                    case "L4":
                        pbLeftFinger4.Image = null;
                        break;
                    case "L5":
                        pbLeftFinger5.Image = null;
                        break;
                }

                al.Add(dr);

            }
            foreach (DataRow dr in al)
                dr.Delete();
        }

        private void tsbSearchUser_Click(object sender, EventArgs e)
        {
            string UserType = "";
            if (cbbUserType.SelectedIndex == 0)
                UserType = "student";
            else if (cbbUserType.SelectedIndex == 1)
                UserType = "staff";
            try
            {
                DataTable dt = DataOperation.GetUserList(UserType, txtUserNumber.Text, txtUserName.Text);

                dgvUsers.DataSource = new BindingSource(dt, null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            tsbSearchUser_Click(null, null);
        }
    }
}
